package org.redmap.net
{
	
	import flash.events.NetStatusEvent;
	import flash.events.EventDispatcher;
	import flash.net.NetConnection;
	
	import mx.collections.ArrayList;
	import mx.controls.Alert;

	/**
	 * @flowerModelElementId _mwMmQKxFEd-xV84SKmH0Yw
	 */
	public class Connection extends EventDispatcher {
		
		private static var connection:Connection = null;
		private var conn:NetConnection = null;
	
		/**
		 * Empty constructor. The Connection class folows the Singleton principle 
		 * so everyone has to use the getInstance().
		 **/
		public function Connection(){	
		}
		
		/**
		 * Static getInstance returns a singleton Connection
		 **/
		 
		public static function getInstance() : Connection{

			if(connection == null){
				return new Connection();
			}

			return connection;
		}
		
		/**
		 * connects to server
		 **/
		public function connect(url:String, user:String, path:String) : void {

			if(conn == null) {
				conn = new NetConnection();
				if (user != null){
					conn.connect(url,user,path);
				}
				else conn.connect(url);
				conn.addEventListener(NetStatusEvent.NET_STATUS, onConnNetStatus);
				//conn.addEventListener(Event.ACTIVATE, onConnActivate);
				//conn.addEventListener(Event.DEACTIVATE, onConnDeactivate);
				//conn.addEventListener(IOErrorEvent.IO_ERROR, onConnIOError);
				//conn.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onConnSecurityError);
			}
			return;
		}

		/**
		 * Gets the connection object
		 * 
		 * @return conn NetConnection
		 **/

		public function getConnection() : NetConnection{
			return conn;
		}

		/**
		 * called after NetStatusEvent is dispatched
		 * 
		 * @param event NetStatusEvent
		 **/

		public function onConnNetStatus(event:NetStatusEvent) : void {			

			// dispatch event
			this.dispatchEvent(event);
		}

	}
}